package com.molichuxing.gateway.bff.goods.service.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.bff.goods.convertor.Convert;
import com.molichuxing.gateway.bff.goods.response.GoodsBaseVo;
import com.molichuxing.gateway.bff.goods.response.GoodsDetailVo;
import com.molichuxing.gateway.bff.goods.response.GoodsVo;
import com.molichuxing.gateway.bff.goods.service.GoodsBffService;
import com.molichuxing.services.business.dto.response.GoodsBaseBizDto;
import com.molichuxing.services.business.dto.response.GoodsBizBizDto;
import com.molichuxing.services.business.service.GoodsBizService;
import com.molichuxing.services.business.service.VehicleBizService;
import com.molichuxing.services.infrastructure.dto.response.VehiclePictureDto;
import com.molichuxing.services.infrastructure.service.VehiclePictureService;
import com.molichuxing.services.property.GoodsTypeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品bff
 * @author xuelin yi
 */
@Service("goodsBffService")
public class GoodsBffServiceImpl implements GoodsBffService {

	@Resource
	private GoodsBizService goodsBizService;

	@Resource
	private VehicleBizService vehicleBizService;

	@Resource
	private VehiclePictureService vehiclePictureService;

	@Override
	public Paged<GoodsVo> getGoodsList(Map<String, Object> selectParams, Integer pageNum, Integer pageSize) {
		if(null == selectParams.get("goodsType")) {
			return null;
		}
		Paged<GoodsBizBizDto> page = goodsBizService.getGoodsList(GoodsTypeEnum.getName(ObjectCastUtil.castInteger(selectParams.get("goodsType"))), ObjectCastUtil.castString(selectParams.get("code")),
				ObjectCastUtil.castInteger(selectParams.get("carStyleId")), ObjectCastUtil.castInteger(selectParams.get("dealerId")), ObjectCastUtil.castInteger(selectParams.get("pageNum")), ObjectCastUtil.castInteger(selectParams.get("pageSize")));
		List<GoodsBizBizDto> goodsList = page.getList();
		Map<Integer, String> vehicleMap = new HashMap<>();
		if(null != goodsList && goodsList.size() > 0) {
			for(GoodsBizBizDto goodsBizBizDto : goodsList) {
				String vehicleName = vehicleBizService.getVehicleName(goodsBizBizDto.getVehicleStyleId());
				vehicleMap.put(goodsBizBizDto.getGoodsId(), vehicleName);
			}
		}
		return Convert.toGoodsPage(page, vehicleMap);
	}

	@Override
	public GoodsBaseVo getBaseGoods(Integer goodsId) throws Exception {
		GoodsBaseBizDto goodsBaseBizDto = goodsBizService.getBaseGoods(goodsId);
		String goodsPicture = null;
		if(null != goodsBaseBizDto) {
			List<VehiclePictureDto> vehiclePictureList = vehiclePictureService.getVehiclePictureByVehicleId(goodsBaseBizDto.getVehicleStyleId());
			if((null != vehiclePictureList)&&(vehiclePictureList.size() > 0)) {
				for(VehiclePictureDto vehiclePictureDto : vehiclePictureList) {
					if(vehiclePictureDto.getIsWhiteBg() && 1 == vehiclePictureDto.getPicDetailType()) {
						goodsPicture = vehiclePictureDto.getServerUrl();
						break;
					}
				}
			}
		}
		return Convert.toGoodsBaseVo(goodsBaseBizDto, goodsPicture);
	}

	@Override
	public GoodsDetailVo getDetailGoods(Integer goodsId) throws Exception {
		return Convert.toGoodsDetailVo(goodsBizService.getDetailGoods(goodsId));
	}
}
